var app = angular.module('reFormApp',[]);
app.controller("reFormController",["$scope",function($scope)
{
	$scope.IsUserNameExist = false;
	$("#username").on("focus",function()
	{
		$scope.IsUserNameExist = false;
		$(".hasSameName").css("display","none");
		$("#usernameOk").css("display","inline-block");
		$("#username").on("blur",function()
		{
			var username = $("#username").val();
			$.get(
				"/usernameExist",
				{username:username},
				function(res)
				{
					console.log(res);
					if(res.state=="0")
					{
						$(".hasSameName").css("display","inline-block");
						$("#usernameOk").css("display","none");
						$scope.IsUserNameExist = true;
					}
				}
			);
		});
	});
	var str = "";
    function randomWord()
    {
        var wordStr = "ABCDEFGHIGKLMNOPQRSTUVWXYZ1234567890";
        var wordArr = wordStr.split("");
        str="";
        for (var i=0;i<4;i++) 
        {
            var randomNum = Math.floor(Math.random()*35+1);
            str+=wordArr[randomNum];
        }
        $(".verification-code").html(str);
    }
    randomWord();
    $(".verification-next").on("click",function()
    {
        randomWord();
    });
    $("#vertification").on("focus",function()
    {
        $("#vertification").on("input",function()
        {
            var inputText =$("#vertification").val().toUpperCase();
//          console.log(inputText)
            if(inputText != str)
            {
                $("#right-img").addClass("right-box-fade").removeClass("right-box-show");
                $(".error-box").last().css("display","inline-block");
                $(".next-button").css({"background-color":" gray","cursor": "default"});
            }else
            {
                $(".error-box").last().css("display","none");
                $("#right-img").removeClass("right-box-fade").addClass("right-box-show");
                $(".next-button").css({"background-color":"#ee0b39","cursor": "pointer"});
            }
        });
    });
    $("#password").on("focus",function()
    {
        $("#password").on("input",function()
        {
            $(".password-strength-gray").css("background-color","#D0D0D0");
            var personPsw = $("#password").val();
            var strongRegex = new RegExp("^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\\W).*$", "g"); 
            var mediumRegex = new RegExp("^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g"); 
            var enoughRegex = new RegExp("(?=.{6,}).*", "g"); 
            if(personPsw.length>0)
            {
                if (false == enoughRegex.test(personPsw)) { 
                    //密码小于六位的时候，密码强度图片都为灰色 
                    $(".password-strength-gray").first().css("background-color","#EE0B39");
                }else if (strongRegex.test(personPsw)) 
                {
                    //密码为八位及以上并且字母数字特殊字符三项都包括,强度最强 
                    $(".password-strength-gray").first().css("background-color","#EE0B39");
                    $(".password-strength-gray:nth-of-type(2)").css("background-color","#FFDD33");
                    $(".password-strength-gray").last().css("background-color","#009900");
                } 
                else if (mediumRegex.test(personPsw)) { 
                    //密码为七位及以上并且字母、数字、特殊字符三项中有两项，强度是中等 
                    $(".password-strength-gray").first().css("background-color","#EE0B39");
                    $(".password-strength-gray:nth-of-type(2)").css("background-color","#FFDD33");
                    $(".password-strength-gray").last().css("background-color","#009900");
                } 
                else { 
                    //如果密码为6为及以下，就算字母、数字、特殊字符三项都包括，强度也是弱的 
                    $(".password-strength-gray").first().css("background-color","#EE0B39");
                } 
            }
        });
    })
    $scope.formSubmit = function()
	{
		if($scope.IsUserNameExist)
		{
			alert("用户名已经存在");
			return;
		}
        if($("#vertification").val().toUpperCase()!= str)
        {
            alert("验证码错误");
            return ;
        }
        $(".main-content-nav>ul>li").removeClass("active-li");
        $(".main-content-nav>ul>li:nth-of-type(2)").addClass("active-li");
        $("form").html("");
		$(".hidden-box").appendTo("form");
		$(".hidden-box").css("display","block");
		
		var telRegex = new RegExp("^1[3|4|5|7|8][0-9]{9}$");
		$("#tel").on("input",function()
		{
			var telValue = $("#tel").val();
			if(false == telRegex.test(telValue))
			{
				$(".error-tel").css("display","inline-block");
				$(".right-tel").css("display","none");
			}else
			{
				$(".error-tel").css("display","none");
				$(".right-tel").css("display","inline-block");
			}
		});
		$(".register").on("click",function()
		{
			if(false == new RegExp("^1[3|4|5|7|8][0-9]{9}$").test($("#tel").val()))
			{
				alert("请填写正确的手机号");
				return;
			}
			if($(".agreement")[0].checked == false)
			{
				alert("请阅读并同意我们的协议");
				return;
			}
			alert("手机号正确，也同意了");
			$(".congrutulation").css("display","block");
			$(".main-content-nav>ul>li").removeClass("active-li");
        	$(".main-content-nav>ul>li:nth-of-type(3)").addClass("active-li");
        	$scope.tel = $("#tel").val();
			$("form").remove();
			//把客户信息发送到服务器，并存储数据库。。
			$.ajax(
			{
				url:'/register',
				type: "POST",
				data:
				{
					username: $scope.username,
					psw: $scope.password,
					tel: $scope.tel
				},
				error:function(err)
				{
					alert("注册失败了");
				},
				success:function(res)
				{
					if(res.state == 1)
					{
//						alert(res.msg);
						$(".congrutulation").html(res.msg);
						return;
					};
					if(res.state == 0)
					{
						$(".congrutulation").html(res.msg+"，3s后跳转到登录页面");
						setTimeout(function()
						{
							location.href = "login.html";
						},3000)
					};
				}
			});
		});
	}
}]);


